import Axios from 'axios'
import { Message } from 'element-ui';
import { getToken } from '@/services/token'

const httpServer = (opts: any) => {
    const httpDefaultOpts = { // http默认配置
        method: opts.method,
        baseURL: 'https://xxxx.com',  // 测试
        url: opts.url,
        timeout: 100000,
        params: opts.params,
        data: opts.params,
        headers: {
            'Content-Type': 'application/json;charset=UTF-8',
            'systoken': '',
        },
    }

    if (getToken()) {
        const token: any = getToken();
        httpDefaultOpts.headers.systoken = token;
    }

    if (opts.method == 'get') {
        delete httpDefaultOpts.data;
    } else {
        delete httpDefaultOpts.params;
    }

    const promise = new Promise((resolve, reject) => {
        Axios(httpDefaultOpts).then(res => {
            resolve(res)
        }).catch(err => {
            reject(err)
        })
    })
    return promise;
}

export default httpServer;